package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qiyou.dao.IUserDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.User;
import com.qiyou.util.LogUtil;

@WebServlet(name = "ClientUserLoginService", urlPatterns = { "/ClientUserLoginService" })
public class ClientUserLoginService extends HttpServlet {
    private static final long serialVersionUID = 3075343789244184921L;
    private static final Logger log = LogUtil
            .getLogger(ClientUserLoginService.class);
    String username = null;
    String pwd = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        username = request.getParameter("username");
        pwd = request.getParameter("password");

        IUserDao userDao = DaoFactory.getUserDao();
        User user = new User();
        user.setID(username);
        user.setPassword(pwd);
        User result = null;

        try {
            result = userDao.getUser(user);
        } catch (SQLException e) {
            log.error(e);
        }

        if (null == result) {
            log.warn("用户:" + username + ", 密码:" + pwd + ", 登录失败");
            out.print("false");
        } else {
            log.info("用户:" + username + ", 密码:" + pwd + ", 登录成功");
            out.print("true");
        }

    }

}
